$(function() {

    //begin列表信息
    $("#potentialCustomer_datagrid").datagrid({
        url: "/potentialCustomer/list",
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "id", title: "编号", width: 100,hidden:true},
            {field: "name", title: "用户名", width: 100},
            {field: "tell", title: "联系电话", width: 100},
            {field: "wechat", title: "微信号", width: 100},

            {field: "school", title: "毕业学校", width: 100},
            {field: "interestDegree", title: "意向程度", width: 100,formatter: function(value,row,index){
                if (row.interestDegree){
                    return row.interestDegree.name;
                } else {
                    return "";
                }
            }},
            {field: "interestCampu", title: "意向校区", width: 100 ,formatter: function(value,row,index){
                if (row.interestCampu){
                    return row.interestCampu.name;
                } else {
                    return "";
                }
            }
            },

            {field: "state", title: "客户状态", width: 100,formatter: function(value,row,index){
                return value==0?'普通客户':'正式学员';
            }},
            {field: "marketer", title: "营销人员", width: 100,formatter: function(value,row,index){
            console.log(value,row,index)
                if (row.marketer){
                    return row.marketer.username;
                } else {
                    return "";
                }
            }},
            {field: "follower", title: "跟踪人员", width: 100,formatter: function(value,row,index){
                if (row.follower){
                    return row.follower.username
                } else {
                    return "";
                }
            }},
            {field: "followTims", title: "跟踪次数", width: 100},
            {field: "lastFollowDate", title: "最后跟踪时间", width: 100},
            {field: "interviewDate", title: "约访时间", width: 100},
            {field: "remark", title: "备注", width: 100}
        ]],
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pageNumber:1,
        pageSize:3,
        pageList:[3,5,10,20],
        toolbar: "#potentialCustomer_tb"
    });
    //end列表信息

    //begin添加/编辑对话框
    $("#potentialCustomer_dialog").dialog({

        width: 1000,
        height: 500,
        buttons: "#potentialCustomer_bb",
        closed: true
    });
    //end添加/编辑对话框


    //begiin跟踪对话框
    $("#customerFollowRecord_dialog").dialog({
        width: 1000,
        height: 500,
        buttons: "#customerFollowRecord_bb",
        closed: true
    });
    //end跟踪对话框

    //begin考试登记对话框
    $("#testRegistration_dialog").dialog({
        width: 500,
        height: 500,
        buttons: "#testRegistration_bb",
        closed: true
    });
    //end考试登记对话框

    //begin学员转正对话框
    $("#positive_dialog").dialog({
        width: 900,
        height: 500,
        buttons: "#positive_bb",
        closed: true
    });
    //end学员转正对话框


    //begin处理功能方法的对象
    var obj = {
        //begin添加按钮
        add: function () {
            $('input').attr('disabled',false);
            $("#potentialCustomer_dialog .easyui-combobox").combobox({ disabled: false })
            $("#myRemark").textbox('readonly',false)
            $("#myCustomerFocus").textbox('readonly',false)

          $("#potentialCustomer_form").form("clear");
            $("#potentialCustomer_dialog").dialog("setTitle", "潜在客户添加");
            $("#potentialCustomer_dialog").dialog("open");
        },
        //end添加按钮

        //begin编辑按钮
        edit: function () {

            $('input').attr('disabled',false);
            $("#potentialCustomer_dialog .easyui-combobox").combobox({ disabled: false })
            $("#myRemark").textbox('readonly',false)
            $("#myCustomerFocus").textbox('readonly',false)

            $("#potentialCustomer_form").form("clear");
            $("#potentialCustomer_dialog").dialog("setTitle", "潜在客户编辑");
            var row = $("#potentialCustomer_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            console.log(row)
            if(row.interestCampu){
                row["interestCampu.id"]=row.interestCampu.id;
            }
            if(row.customerSource){
                row["customerSource.id"]=row.customerSource.id;
            }
            if(row.education){
                row["education.id"]=row.education.id;
            }
            if(row.regional){
                row["regional.id"]=row.regional.id;
            }
            if(row.interestDegree){
                row["interestDegree.id"]=row.interestDegree.id;
            }

            if(row.interestClass){
                row["interestClass.id"]=row.interestClass.id;
            }
            if(row.introducer){
                row["introducer.id"]=row.introducer.id;
            }
            if(row.interestSubject){
                row["interestSubject.id"]=row.interestSubject.id;
            }
            if(row.schoolCustomer){
                row["schoolCustomer.id"]=row.schoolCustomer.id;
            }
            $("#potentialCustomer_form").form("load", row);

            $("#potentialCustomer_dialog").dialog("open");
        },
        //end编辑按钮

        //begin查看按钮
        view: function () {

            $('input').attr('disabled',true);
            $("#potentialCustomer_dialog .easyui-combobox").combobox({ disabled: true })
            $("#myRemark").textbox('readonly',true)
            $("#myCustomerFocus").textbox('readonly',true)


            $("#potentialCustomer_form").form("clear");
            $("#potentialCustomer_dialog").dialog("setTitle", "潜在客户查看");
            var row = $("#potentialCustomer_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            if(row.interestCampu){
                row["interestCampu.id"]=row.interestCampu.id;
            }
            if(row.customerSource){
                row["customerSource.id"]=row.customerSource.id;
            }
            if(row.education){
                row["education.id"]=row.education.id;
            }
            if(row.regional){
                row["regional.id"]=row.regional.id;
            }
            if(row.interestDegree){
                row["interestDegree.id"]=row.interestDegree.id;
            }

            if(row.interestClass){
                row["interestClass.id"]=row.interestClass.id;
            }
            if(row.introducer){
                row["introducer.id"]=row.introducer.id;
            }
            if(row.interestSubject){
                row["interestSubject.id"]=row.interestSubject.id;
            }
            if(row.schoolCustomer){
                row["schoolCustomer.id"]=row.schoolCustomer.id;
            }

            $("#potentialCustomer_form").form("load", row);
            $("#potentialCustomer_dialog").dialog("open");

        },
        //end查看按钮



        //begin取消按钮
        cancel: function () {
            $("#potentialCustomer_dialog").dialog("close");
        },
        //end取消按钮

        //begin保存按钮
        save: function () {
            var potentialCustomerId = $("#potentialCustomerId").val();
            var url;
            if (potentialCustomerId) {
                url = "/potentialCustomer/update"
            } else {
                url = "/potentialCustomer/save"
            }
            $("#potentialCustomer_form").form("submit", {
                url: url,
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        $("#potentialCustomer_dialog").dialog("close");
                        $("#potentialCustomer_datagrid").datagrid("reload");
                    }
                }
            });
        },
        //end保存按钮

        //begin删除按钮
        remove:function(){
            var row = $("#potentialCustomer_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $.messager.confirm('确认','您确认要操作此行吗？',function(r){
                if (r){
                    //注意这里的id要从当前选中的行中获取
                    var potentialCustomerId = row.id;
                    $.get("/potentialCustomer/delete",{potentialCustomerId:potentialCustomerId},function(data){
                        if(!data.success){
                            $.messager.alert('温馨提示', data.msg);
                        }else{
                            $.messager.alert('温馨提示', '操作成功!');
                            $("#potentialCustomer_datagrid").datagrid("reload");
                        }
                    });
                }
            });

        },
        //end删除按钮

        //begin刷新按钮
        reload:function (){
            $("#potentialCustomer_datagrid").datagrid("reload")
        },
        //end刷新按钮

        //begin跟踪按钮
        follow:function () {
            $("#customerFollowRecord_form").form("clear");
           var row= $("#potentialCustomer_datagrid").datagrid("getSelected");
           if(!row){
               $.messager.alert('温馨提示', '请选择要跟踪的客户!');
               return;
           }
            if(row.follower){
                row["follower.id"]=row.follower.id;
            }
            if(row.interestClass){
                row["interestClass.id"]=row.interestClass.id;
            }

            if(row.interestDegree){
                row["interestDegree.id"]=row.interestDegree.id;
            }

           $("#customerFollowRecord_form").form("load", row);
            $("#customerFollowRecord_dialog").dialog("open");

        },
        //end跟踪按钮

        //begin关闭跟踪对话框
        customerFollowRecord_cancel:function () {
            $("#customerFollowRecord_dialog").dialog("close")
        },
        //end关闭跟踪对话框

        //begin跟踪的提交按钮
        customerFollowRecord_commit:function () {
            $("#customerFollowRecord_form").form("submit", {
                url:"/customerFollowRecord/save",
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        $("#customerFollowRecord_dialog").dialog("close");
                        $("#potentialCustomer_datagrid").datagrid("reload");
                    }
                }
            })
        },
        //end跟踪的提交按钮

        //begin放进资源池按钮
        putToPool:function () {
          var row= $("#potentialCustomer_datagrid").datagrid("getSelected");
          if(!row){
              $.messager.alert('温馨提示', "请选择要放入资源池的客户");
              return;
          }
            $.messager.confirm('确认','您确定要放入到资源池么？',function(r){
                if (r){
                    $.get("/potentialCustomer/putlToPool?id="+row.id,function (data) {
                       if(data.success){
                           var row= $("#potentialCustomer_datagrid").datagrid("reload");
                       }else {
                           $.messager.alert('温馨提示', "操作失败");
                       }
                    })
                }
            });
        },
        //end放进资源池按钮

        //begin考试登记按钮
        registration:function () {
         var row= $("#potentialCustomer_datagrid").datagrid("getSelected");
         if(!row){
             $.messager.alert('温馨提示', "请选择要登记考试的客户");
             return
         }
         console.log(row)
        $("#testRegistration_form").form("clear");
            if(row.interestClass){
                row["interestClass.id"]=row.interestClass.id;
            }
            if(row.testType){
                row["testType.id"]=row.testType.id;
            }
        $("#testRegistration_form").form("load",row);
        $("#testRegistration_dialog").dialog("open")

        },
        //end考试登记按钮

        //begin学员转正按钮
        positive:function () {
         var row= $("#potentialCustomer_datagrid").datagrid("getSelected");
        if(row.state==1){
            $.messager.alert('温馨提示', "对不起,操作失败,该客户已经是正式学员");
            return;
        }


         if(!row){
             $.messager.alert('温馨提示', "请选择要转正的学员");
             return
         }
        $("#positive_form").form("clear");
            if(row.interestCampu){
                row["interestCampu.id"]=row.interestCampu.id;
            }
            if(row.customerSource){
                row["customerSource.id"]=row.customerSource.id;
            }
            if(row.marketer){
                row["marketer.id"]=row.marketer.id;
            }
            if(row.schoolCustomer){
                row["schoolCustomer.id"]=row.schoolCustomer.id;
            }
            if(row.education){
                row["education.id"]=row.education.id;
            }
            if(row.interestClass){
                row["interestClass.id"]=row.interestClass.id;
            }
            if(row.intentionjob){
                row["intentionjob.id"]=row.intentionjob.id;
            }
            if(row.intentionjob){
                row["intentionjob.id"]=row.intentionjob.id;
            }
        $("#positive_form").form("load",row);
        $("#positive_dialog").dialog("open")
        },
        //end学员转正按钮

        //begin考试登记框的取消按钮
        testRegistration_cancel:function () {
            $("#testRegistration_dialog").dialog("close")
        },
        //end考试登记框的取消按钮

        //begin考试登记框提交按钮
        testRegistration_commit:function () {
            $("#testRegistration_form").form("submit",{
                url:"/potentialCustomer/tsetRegistration",
                success:function (data) {
                    data= $.parseJSON(data)
                   if(data.success){
                       $.messager.alert('温馨提示', "操作成功");
                       $("#testRegistration_dialog").dialog("close")
                       $("#potentialCustomer_datagrid").datagrid("reload");
                   }else {
                       $.messager.alert('温馨提示', "操作失败");
                   }
                }
            })
        },
        //end考试登记框提交按钮

        //begin学员转正框提交按钮
        positive_commit:function () {
            $("#positive_form").form("submit",{
                url:"/potentialCustomer/positive",
                success:function (data) {
                    data= $.parseJSON(data)
                   if(data.success){
                       $.messager.alert('温馨提示', "操作成功");
                       $("#positive_dialog").dialog("close")
                       $("#positive_datagrid").datagrid("reload");
                   }else {
                       $.messager.alert('温馨提示', "操作失败");
                   }
                }
            })
        },
        //end学员转正框提交按钮

        //begin学员转正框的取消按钮
        positive_cancel:function () {
            $("#positive_dialog").dialog("close")
        },
        //end学员转正框的取消按钮

        //begin导出
        exportJxl:function () {
            $.messager.confirm('确认','亲,您确定要导出么?',function(r){
                if (r){
                   $.get("/potentialCustomer/exportJxl",function (data) {
                       if(data.success){
                           $.messager.alert('温馨提示', "亲,已经帮你导出到D盘,文件为exportJxl.xls");
                       }else {
                           $.messager.alert('温馨提示',data.msg );
                       }
                   })
                }
            });
        }
        //end导出
        

    };


    //begin处理功能方法对象,实际调用方法
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        obj[cmd]();
    });
    //end处理功能方法对象,实际调用方法


});

//begin高级查询
$(function () {

    $("#btn_search").linkbutton({
        iconCls:"icon-search",
        text:"搜索",
        onClick:function () {
            var keywords=$("#keywords").textbox("getValue");
            var  beginDate=$("#beginDate").datebox("getValue");
            var  endDate=$("#endDate").datebox("getValue");
            $("#potentialCustomer_datagrid").datagrid("load",{
                keywords:keywords,
                beginDate:beginDate,
                endDate:endDate
            })

        }
    })
})


//end高级查询





